package com.xu.demo.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xu.demo.common.R;
import com.xu.demo.entity.User;
import com.xu.demo.service.IUserService;
import org.apache.commons.lang.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xu.demo.entity.Student;
import com.xu.demo.entity.Teacher;
import com.xu.demo.service.StudentService;
import com.xu.demo.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@CrossOrigin
@RestController
@RequestMapping("/User")
public class UserController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private IUserService userService;

    @GetMapping("/page")
    public Page page(int pageNum, int pageSize, String name) {

//        构造分页构造器
        Page pageInfo = new Page(pageNum, pageSize);
//        构造条件构造器
        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.like(name != null, Student::getS_name, name);
        queryWrapper.orderByDesc(Student::getS_id);
        //执行查询
        studentService.page(pageInfo, queryWrapper);
        System.out.println(pageInfo.getRecords().toString());
        return pageInfo;
    }

    @PostMapping
    public boolean save(@RequestBody Student student) {
        System.out.println(student.toString());
        return studentService.saveOrUpdate(student);
    }

    @DeleteMapping("/{id}")
    public boolean del(@PathVariable Integer id) {
        System.out.println();
        return studentService.removeById(id);
    }

    @PostMapping("/login")
    public R<User> login(HttpServletRequest request, @RequestBody User user) {
        String id = String.valueOf(user.getId());
        String password = user.getPassword();
        if (StringUtils.isBlank(id) || StringUtils.isBlank(password)) {
            return R.error("账号或者密码为空");
        }
        return userService.login(request,user);
    }
    @PostMapping("/update")
    public R<String> update(@RequestBody User user){
        System.out.println(user.toString());
        return R.success("保存成功");
    }
}

